{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Viola-Jones Example"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Prerequisits"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Install viola-jones package by running:\n",
    "\n",
    "    pip install -e <path to viola-jones>"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Imports"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "import violajones.IntegralImage as ii\n",
    "import violajones.AdaBoost as ab\n",
    "import violajones.Utils as utils\n",
    "from IPython.core.display import Image, display"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Config"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "pos_training_path = 'trainingdata/faces'\n",
    "neg_training_path = 'trainingdata/nonfaces'\n",
    "pos_testing_path = 'trainingdata/faces/test'\n",
    "neg_testing_path = 'trainingdata/nonfaces/test'\n",
    "\n",
    "num_classifiers = 20\n",
    "# For performance reasons restricting feature size\n",
    "min_feature_height = 4\n",
    "max_feature_height = 10\n",
    "min_feature_width = 4\n",
    "max_feature_width = 10"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Load images"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Loading faces for training..\n",
      "..done. 10 faces loaded.\n",
      "\n",
      "Loading non faces..\n",
      "..done. 10 non faces loaded.\n",
      "\n",
      "Loading faces for testing..\n",
      "..done. 279 faces loaded.\n",
      "\n",
      "Loading test non faces..\n",
      "..done. 215 non faces loaded.\n",
      "\n"
     ]
    }
   ],
   "source": [
    "print('Loading faces for training..')\n",
    "faces_training = utils.load_images(pos_training_path)\n",
    "faces_ii_training = list(map(ii.to_integral_image, faces_training))\n",
    "print('..done. ' + str(len(faces_training)) + ' faces loaded.\\n\\nLoading non faces..')\n",
    "non_faces_training = utils.load_images(neg_training_path)\n",
    "non_faces_ii_training = list(map(ii.to_integral_image, non_faces_training))\n",
    "print('..done. ' + str(len(non_faces_training)) + ' non faces loaded.\\n')\n",
    "\n",
    "print('Loading faces for testing..')\n",
    "faces_testing = utils.load_images(pos_testing_path)\n",
    "faces_ii_testing = list(map(ii.to_integral_image, faces_testing))\n",
    "print('..done. ' + str(len(faces_testing)) + ' faces loaded.\\n\\nLoading test non faces..')\n",
    "non_faces_testing = utils.load_images(neg_testing_path)\n",
    "non_faces_ii_testing = list(map(ii.to_integral_image, non_faces_testing))\n",
    "print('..done. ' + str(len(non_faces_testing)) + ' non faces loaded.\\n')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Learn (boost) classifiers (Haar-Like features)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Creating haar-like features..\n",
      "..done. 49122 features created.\n",
      "\n",
      "Calculating scores for images..\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "100% (20 of 20) |#########################| Elapsed Time: 0:00:11 Time: 0:00:11\n",
      "N/A% (0 of 20) |                         | Elapsed Time: 0:00:00 ETA:  --:--:--"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Selecting classifiers..\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "100% (20 of 20) |#########################| Elapsed Time: 0:00:18 Time: 0:00:18\n"
     ]
    }
   ],
   "source": [
    "# This will take a while\n",
    "classifiers = ab.learn(faces_ii_training, non_faces_ii_training, num_classifiers, min_feature_height, max_feature_height, min_feature_width, max_feature_width)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Testing"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Testing selected classifiers..\n",
      "..done.\n",
      "\n",
      "Result:\n",
      "      Faces: 209/279  (74.91039426523297%)\n",
      "  non-Faces: 98/215  (45.58139534883721%)\n"
     ]
    }
   ],
   "source": [
    "print('Testing selected classifiers..')\n",
    "correct_faces = 0\n",
    "correct_non_faces = 0\n",
    "correct_faces = sum(utils.ensemble_vote_all(faces_ii_testing, classifiers))\n",
    "correct_non_faces = len(non_faces_testing) - sum(utils.ensemble_vote_all(non_faces_ii_testing, classifiers))\n",
    "\n",
    "print('..done.\\n\\nResult:\\n      Faces: ' + str(correct_faces) + '/' + str(len(faces_testing))\n",
    "      + '  (' + str((float(correct_faces) / len(faces_testing)) * 100) + '%)\\n  non-Faces: '\n",
    "      + str(correct_non_faces) + '/' + str(len(non_faces_testing)) + '  ('\n",
    "      + str((float(correct_non_faces) / len(non_faces_testing)) * 100) + '%)')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.5.2"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
